package com.cloudkinto.service.dic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cloudkinto.common.DicBusinessConstant;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.entity.DicBusinessItemNewDo;
import com.cloudkinto.dao.DicBusinessItemNewDao;
import com.cloudkinto.service.dic.DicBusinessItemNewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author ding
 * @since 2023-06-14
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class DicBusinessItemNewServiceImpl extends ServiceImpl<DicBusinessItemNewDao, DicBusinessItemNewDo> implements DicBusinessItemNewService {
    @Autowired
    private DicBusinessItemNewDao dao;

    @Override
    public SingleResult addInit() {
        return SingleResult.success("");
    }

    @Override
    public Object add(Object req, Long userId) {
        DicBusinessItemNewDo entityDo = new DicBusinessItemNewDo();
        BeanUtils.copyProperties(req, entityDo);
        handlerAddBefore(entityDo, userId);
        dao.insert(entityDo);
        return handlerAddAfter(entityDo);
    }

    //新增校验
    private void addCheck(Object req) {

    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(DicBusinessItemNewDo entityDo, Long userId) {
        entityDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
    }

    //将新增的do转换成要返回的实体
    private Object handlerAddAfter(DicBusinessItemNewDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public SingleResult updateInit(Long id) {
        return SingleResult.success("");
    }

    @Override
    public Object update(Object req, Long userId) {
        /*DicBusinessItemNewDo entityDo = dao.selectById(req.getId());
        BeanUtils.copyProperties(req, entityDo);
        handlerUpdateBefore(entityDo, userId);
        dao.updateById(entityDo);
        return handlerUpdateAfter(entityDo);*/
        return null;
    }

    //编辑校验
    private void updateCheck(Object req) {

    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(DicBusinessItemNewDo entityDo, Long userId) {

    }

    //做修改之后要执行的业务定制处理
    private Object handlerUpdateAfter(DicBusinessItemNewDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public int delete(Long id, Long userId) {
        DicBusinessItemNewDo entityDo = dao.selectById(id);
        handlerDeleteBefore(entityDo, userId);
        return dao.deleteById(id);
    }

    //做删除之前要执行的业务定制处理
    private void handlerDeleteBefore(DicBusinessItemNewDo entityDo, Long userId) {
    }

    @Override
    public Object detail(Long id) {
        DicBusinessItemNewDo entityDo = dao.selectById(id);
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return handlerDetailAfter(res);
    }

    //查询明细之后要执行的业务定制处理
    private Object handlerDetailAfter(Object res) {
        return res;
    }

    @Override
    public SingleResult pageInit() {
        return SingleResult.success("");
    }

    @Override
    public List<Object> getList(Map<String, String> map) {
        QueryWrapper<DicBusinessItemNewDo> wrapper = queryBuild(map);
        List<DicBusinessItemNewDo> list = this.list(wrapper);
        List<Object> result = handlerListAfter(list);
        return result;
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<DicBusinessItemNewDo> wrapper = queryBuild(map);
        IPage<DicBusinessItemNewDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<Object> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<Object> handlerListAfter(List<DicBusinessItemNewDo> list) {
        List<Object> result = new ArrayList<>();
        return result;
    }

    private IPage<DicBusinessItemNewDo> pageInit(Map<String, String> map) {
        IPage<DicBusinessItemNewDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<DicBusinessItemNewDo> queryBuild(Map<String, String> map) {
        QueryWrapper<DicBusinessItemNewDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.like("keyword", map.get("keyword"));
        }
        return wrapper;
    }


    @Override
    public void insertItemNewDo(Long dicBusinessId, String value, Long companyId) {
        if (StringUtils.isBlank(value)) {
            return;
        }
        DicBusinessItemNewDo item1 = dao.selectOne(new LambdaQueryWrapper<DicBusinessItemNewDo>()
                .eq(DicBusinessItemNewDo::getDicBusinessId, dicBusinessId).eq(DicBusinessItemNewDo::getCompanyId, companyId)
                .eq(DicBusinessItemNewDo::getDicItemValue, value).last(" limit 1"));
        if (item1 == null) {
            item1 = new DicBusinessItemNewDo();
            item1.setCompanyId(companyId);
            item1.setDicItemValue(value);
            item1.setDicBusinessId(dicBusinessId);
            dao.insert(item1);
        }
    }


    //维修管理 标签初始化
    @Override
    public List<Map> repairCategoryInit(Long companyId) {
        if (companyId == null) {
            throw new BizException(SysConstant.No_Company_Info);
        }
        this.insertItemNewDo(DicBusinessConstant.repairLabelCanNotEdit, "更换主板", companyId);
        this.insertItemNewDo(DicBusinessConstant.repairLabelCanNotEdit, "更换电机", companyId);
        this.insertItemNewDo(DicBusinessConstant.repairLabelCanNotEdit, "更换水箱", companyId);
        this.insertItemNewDo(DicBusinessConstant.repairLabelCanNotEdit, "系统运转不良", companyId);

        List<Map> accountType = new ArrayList<>();
        List<DicBusinessItemNewDo> list = dao.selectList(new LambdaQueryWrapper<DicBusinessItemNewDo>()
                .and(i -> i.eq(DicBusinessItemNewDo::getDicBusinessId, DicBusinessConstant.repairLabelCanNotEdit).or().eq(DicBusinessItemNewDo::getDicBusinessId, DicBusinessConstant.repairLabelCanEdit))
                .eq(DicBusinessItemNewDo::getCompanyId, companyId)
        );
        list.forEach(et -> {
            Map map = new HashMap();
            map.put("id", et.getId());
            map.put("value", et.getDicItemValue());
            map.put("type", DicBusinessConstant.repairLabelCanNotEdit.equals(et.getDicBusinessId()) ? 0 : 1);
            accountType.add(map);
        });
        return accountType;
    }


}
